<template>
  <div class="diy-index flex">
    <!-- 数组类型 -->
    <div :class="isArr ? 'left true' : 'left false'">
      <template v-if="isArr">
        <!-- 多个图文 -->
        <diyImg :data="data" v-if="type == 'imgs'" @pickImg="pickImg" />

        <!-- 键值对 -->
        <diyKeyval :data="data" v-if="type == 'keyVal'" />
      </template>

      <!-- 非数组 -->
      <template v-else>
        <!-- 普通文本 -->
        <el-input
          v-if="type == 'text'"
          v-model.trim="data.value"
          type="text"
          placeholder="请输入"
          clearable
        ></el-input>

        <!-- 多个图文 -->
        <diyImg :data="data.value" v-if="type == 'img'" @pickImg="pickImg" />

        <!-- 富文本 -->
        <div v-if="type == 'editor'">
          <el-button type="primary" @click="openEditor()"
            >编辑 / 查看内容</el-button
          >
        </div>
      </template>
    </div>

    <div class="right" v-if="isArr">
      <el-tooltip
        class="box-item"
        effect="dark"
        content="移除当前项，保存后生效"
        placement="left"
      >
        <i class="iconfont" @click="remove()">&#xe7cc;</i>
      </el-tooltip>
    </div>
  </div>
</template>

<script lang="ts" setup>
import diyKeyval from "./diyKeyval.vue";
import diyImg from "./diyImg.vue";

const emit = defineEmits<{
  (e: "pickImg",index:number): void;
  (e: "remove"): void;
  (e: "openEditor"): void;
}>();
interface IProps {
  data: any;
  type: string;
  isArr: boolean;
}
defineProps<IProps>();

function pickImg(index:number) {
  emit("pickImg",index);
}

function openEditor() {
  emit("openEditor");
}

function remove() {
  emit("remove");
}
</script>

<style lang="scss" scoped>
.diy-index {
  flex: 1;
  margin: 0;
  .el-input {
    flex: 1;
  }
  .left {
    // margin-right: 10px;
    flex: 1;
    padding: 5px;
    &.true {
      cursor: move;
      width: 100%;
      &:hover {
        background: var(--el-color-primary-light-9);
      }
    }
  }
  .right {
    .iconfont:hover {
      color: var(--el-color-primary);
      cursor: pointer;
    }
  }
}
@media screen and (max-width: 1400px) {
  .diy-index {
    margin: 0 10px;
  }
}
</style>
